#include "SoundTab.h"
#include "MyWindow.h"

#include <QSize>

SoundTab::SoundTab(SoundObj *so, QWidget *parent):QWidget(parent)
{
	Q_UNUSED(parent);

	mainLayout = new QVBoxLayout;

	this->nbrChannel = 0;

	buildEmptyBlock();
	mainLayout->addWidget(nosoundWidget);

	setLayout(mainLayout);
}

void SoundTab::buildEmptyBlock()
{
	nosoundWidget = new QWidget;
	nosoundLabel = new QLabel(tr("No sound is opened"));
	nosoundLabel->setAlignment(Qt::AlignCenter);

	QVBoxLayout *layout = new QVBoxLayout;
	layout->setMargin(5);
	layout->addWidget(nosoundLabel);
	nosoundWidget->setLayout(layout);
}

void SoundTab::buildPlayer()
{
	blocPlayer = new QGroupBox;
	QHBoxLayout *iconsPlayer = new QHBoxLayout();
	pause = new QPushButton(QIcon("../img/pause.png"),"");
	stop = new QPushButton(QIcon("../img/stop.png"),"");
	start = new QPushButton(QIcon("../img/play.png"),"");
	iconsPlayer -> addWidget(pause);
	iconsPlayer -> addWidget(stop);
	iconsPlayer -> addWidget(start);
	blocPlayer->setLayout(iconsPlayer);

	mainLayout->addWidget(blocPlayer);

	actionsOnButtons();
}

void SoundTab::buildBlocByChannel(long id)
{
	QSize size(200, 500);

	scrollArea[id]   = new QScrollArea;
	blocksBox[id]    = new QGroupBox;
	blocksLayout[id] = new QVBoxLayout;
	blocksSelect[id] = new QComboBox;
	blocksSelect[id]->setMaximumWidth(140);

	for (long i=1; i<=this->nbrChannel; i++)
	{
		blocksSelect[id]->addItem(tr("Channel ") + QString::number(i));
	}
	blocksSelect[id]->setCurrentIndex(id);

	SoundPlots[id] = new SoundPlot(this->soundObj, id+1, this);

	connect(blocksSelect[id], SIGNAL(currentIndexChanged(int)), SoundPlots[id], SLOT(rebuildBlocByChannel(int)));

	blocksLayout[id]->addWidget(blocksSelect[id]);
	blocksLayout[id]->addWidget(SoundPlots[id]);

	blocksBox[id]->setLayout(blocksLayout[id]);
	scrollArea[id]->setWidget(blocksBox[id]);
	scrollArea[id]->setAlignment(Qt::AlignCenter);
	scrollArea[id]->setMaximumSize(600, 400);
}

void SoundTab::repaint()
{
	for (long i=0; i<this->nbrChannel; i++)
	{
		blocksBox[i]->setTitle(tr("Channel ") + QString::number(i+1));
		blocksSelect[i]->setCurrentIndex(i);
	}
}

void SoundTab::buildBlocks()
{
	clear();

	GridLayout = new QGridLayout;

	blocksLayout = new QVBoxLayout *[this->nbrChannel];
	blocksBox    = new QGroupBox   *[this->nbrChannel];
	blocksSelect = new QComboBox   *[this->nbrChannel];
	SoundPlots   = new SoundPlot   *[this->nbrChannel];
	scrollArea   = new QScrollArea *[this->nbrChannel];

	for (long i=0; i<this->nbrChannel; i++)
	{
		buildBlocByChannel(i);

		GridLayout->addWidget(scrollArea[i], i%3, (long) floor(i/3.0));
	}

	GroupBoxBloc = new QGroupBox;
	GroupBoxBloc->setLayout(GridLayout);

	mainLayout->addWidget(GroupBoxBloc);

	buildPlayer();
}

void SoundTab::actionsOnButtons()
{
	connect(start, SIGNAL(clicked()), this, SLOT(startSound()));
	connect(stop, SIGNAL(clicked()), this, SLOT(stopSound()));
	connect(pause, SIGNAL(clicked()), this, SLOT(pauseSound()));
}

void SoundTab::startSound()
{
	sound -> play();
}
void SoundTab::stopSound()
{
	sound -> stop();
}
void SoundTab::pauseSound()
{
	sound -> stop();
}

void SoundTab::setSound(QSound *sound2)
{
	this->sound = sound2;
}

void SoundTab::setSoundObj(SoundObj *so)
{
	if (this->nbrChannel) tmpSO = this->soundObj;
	this->soundObj = so;
}

void SoundTab::clear()
{
	if (this->nbrChannel)
	{
		for (long i=0; i<this->nbrChannel; i++)
		{
			mainLayout->removeWidget(scrollArea[i]);
			delete blocksLayout[i];
			delete blocksSelect[i];
			delete SoundPlots[i];
			delete blocksBox[i];
			delete scrollArea[i];
		}
		delete GridLayout;
		mainLayout->removeWidget(GroupBoxBloc);

		delete blocPlayer;
	}
	else
	{
		mainLayout->removeWidget(nosoundWidget);
		delete nosoundWidget;
	}
	this->nbrChannel = this->soundObj->getNbrOfChannels();
}

SoundTab::~SoundTab()
{
}
